<template>
  <div id="pinche">
    <div class="field_box">
      <md-field>
        <md-input-item
          :value="startInfoString"
          class="no-right-padding"
          ref="startLocation"
          :placeholder="getRoute?getRoute.start_auto_name +'  从哪出发':''"
          readonly
          @click.native="selectStart"
        >
          <md-icon class="place_icon" name="shangchedian" slot="left"></md-icon>
          <div v-if="false" slot="right" class="out_tag">
            不在服务范围内
            <md-icon class="tk_close" name="tk_close"></md-icon>
          </div>
        </md-input-item>
        <md-input-item
          :value="endInfoString"
          class="no-right-padding"
          ref="endLocation"
          :placeholder="getRoute?getRoute.end_auto_name + '  您要去哪':''"
          readonly
          @click.native="selectEnd"
        >
          <md-icon class="place_icon" name="xiachedian" slot="left"></md-icon>
          <div v-if="false" slot="right" class="out_tag">
            不在服务范围内
            <md-icon class="tk_close" name="tk_close"></md-icon>
          </div>
        </md-input-item>
        <md-input-item
          :value="getBaoStartTime?getBaoStartTime.timeStr:''"
          v-if="positionSelected"
          ref="startTime"
          placeholder="出发时间"
          readonly
          @click.native="selectStartTime"
        >
          <md-icon class="place_icon" name="time" slot="left"></md-icon>
        </md-input-item>
        <md-input-item
          :value="highwayString"
          @click.native="selectHighway"
          ref="useHighway"
          v-if="getRoute?getRoute.hspCostFlag=='0':false"
          v-show="positionSelected"
          placeholder="是否走高速"
          readonly
        >
          <md-icon class="place_icon" name="gaosu" slot="left"></md-icon>
        </md-input-item>
        <md-input-item
          v-if="getRoute?getRoute.shangwuFlag!='1':false"
          v-show="positionSelected"
          @click.native="selectRemarks"
          ref="remarks"
          placeholder="出行备注"
          readonly
          :value="getBaoSelectedRemarks.length>0?'已添加':''"
        >
          <md-icon class="place_icon" name="chuxingbeizhu" slot="left"></md-icon>
        </md-input-item>
        <md-field-item :customized="true" v-if="getRoute?getRoute.shangwuFlag=='1':false" v-show="positionSelected">
          <md-input-item :value="getBaoCarType" @click.native="selectCartype" class="inline_field" ref="carType" placeholder="公务车" readonly slot="left">
            <md-icon class="place_icon" name="bc_cx" slot="left"></md-icon>
          </md-input-item>
          <md-input-item
            @click.native="selectRemarks"
            class="inline_field"
            ref="remarks"
            placeholder="出行备注"
            readonly
            slot="right"
            :value="getBaoSelectedRemarks.length>0?'已添加':''"
          >
            <md-icon class="place_icon" name="chuxingbeizhu" slot="left"></md-icon>
          </md-input-item>
        </md-field-item>
        <div class="cost-info" v-show="getBaoCostInfo">
          <div class="row centered">
            <div class="price">
              <span class="symbol">￥</span>
              {{getBaoCostInfo?getBaoCostInfo.prefePrice:''}}
            </div>
            <md-icon name="wdqb-more" @click="gotoCostDetail"></md-icon>
          </div>
          <div class="desc" v-if="getBaoCostInfo&&getBaoCostInfo.cheapFlag=='1'">
            原价{{getBaoCostInfo.price}}元，卷已抵
            <span style="color:#FF6917">{{getBaoCostInfo.cheapPrice}}</span>元
          </div>
          <div class="gaomsg" v-if="getBaoCostInfo?getBaoCostInfo.gaosuMsg:''">
            <img src="@/assets/imgs/tk_ts.png" />
            {{getBaoCostInfo?getBaoCostInfo.gaosuMsg:""}}
          </div>
        </div>
        <md-button class="submit-btn" @click="submitOrder" v-if="positionSelected" :disabled="!getBaoCostInfo">提交订单</md-button>
      </md-field>
    </div>
    <div class="adv" v-if="getBaoAdv?getBaoAdv.url!='':false">
      <a :href="getBaoAdv?getBaoAdv.url:''" target="_blank">
        <img class="img" :src="getBaoAdv?getBaoAdv.img:''" />
      </a>
    </div>
  </div>
</template>

<script>
import '@/assets/svgs/shangchedian.svg';
import '@/assets/svgs/xiachedian.svg';
import '@/assets/svgs/time.svg';
import '@/assets/svgs/chuxingbeizhu.svg';
import '@/assets/svgs/gaosu.svg';
import '@/assets/svgs/bc_cx.svg';
import '@/assets/svgs/wdqb-more.svg';
import { mapGetters } from 'vuex';
import { Field, FieldItem, Icon, InputItem, Button, Tag, Dialog } from 'mand-mobile';
import Popup from './popup';
import Map from '@/components/map';
import { decryptByDESModeEBC, encryptByDESModeEBC } from '@/common/encrypt';
import { COST_ESTIMAT_CHARTER, ORDER_SUBMIT, GET_ORDER_TIME } from '@/store/actions.types';
import Utils from '@/common/pakjUtils';
var moment = require('moment');
export default {
  name: 'baoche',
  data() {
    return {
      positionSelected: false,
      promised: false,
      hasCostInfo: false,
      highwayDefault: {
        text: '不走高速',
        value: '0'
      },
      carTypeDefault: {
        text: '公务车',
        value: '1'
      }
    };
  },
  components: {
    [Field.name]: Field,
    [FieldItem.name]: FieldItem,
    [Icon.name]: Icon,
    [InputItem.name]: InputItem,
    [Button.name]: Button,
    Tag
  },
  updated() {
    if (this.getBaoStartInfo && this.getBaoEndInfo) {
      this.positionSelected = true;
    } else {
      this.positionSelected = false;
    }
  },
  created() {
    if (this.getBaoStartInfo && this.getBaoEndInfo) {
      this.positionSelected = true;
    } else {
      this.positionSelected = false;
    }
    if (this.getBaoCostInfo) {
      this.hasCostInfo = true;
    } else {
      this.hasCostInfo = false;
    }
  },
  methods: {
    selectStart() {
      Map.createMap(true, '2', this.getRoute.bao_opt_type_start)
        .then(() => {
          if (this.getBaoEndInfo && !this.promised) {
            this.startPromise();
          } else {
            if (this.promised) {
              this.getCostInfo();
            }
          }
        })
        .catch(reason => {
          if (reason.message === 'pin') {
            this.$emit(reason.message);
          }
        });
    },
    selectEnd() {
      Map.createMap(false, '2', this.getRoute.bao_opt_type_end)
        .then(() => {
          if (this.getBaoStartInfo && !this.promised) {
            this.startPromise();
          } else {
            if (this.promised) {
              this.getCostInfo();
            }
          }
        })
        .catch(reason => {
          if (reason.message === 'pin') {
            this.$emit(reason.message);
          }
        });
    },
    startPromise() {
      this.$store.dispatch(GET_ORDER_TIME, '包车').then(() => {
        Popup.createTimeSelector().then(() => {
          new Promise(resolve => {
            if (this.getRoute.hspCostFlag === '0') {
              Popup.createHighwaySelector().then(() => {
                resolve();
              });
            } else {
              resolve();
            }
          }).then(() => {
            new Promise(resolve => {
              if (this.getRoute.shangwuFlag === '1') {
                Popup.createCartypeSelector().then(() => {
                  resolve();
                });
              } else {
                resolve();
              }
            }).then(() => {
              this.promised = true;
              this.getCostInfo();
              Popup.createRemarkSelector('2');
            });
          });
        });
      });
    },
    getCostInfo() {
      if (!this.canGetCostInfo) {
        return;
      }
      let params = {
        route_id: this.getRoute.route_id,
        origin_coord: encryptByDESModeEBC(this.getBaoStartInfo.coordinate[0] + ',' + this.getBaoStartInfo.coordinate[1]),
        dest_coord: encryptByDESModeEBC(this.getBaoEndInfo.coordinate[0] + ',' + this.getBaoEndInfo.coordinate[1]),
        travel_time: this.getBaoStartTime.time.valueOf().toString(),
        car_type: this.getBaoCarType,
        speed_status: this.getUseHighway.value
      };
      if (this.getBaoStartInfo.routeStationId) {
        params.start_site = this.getBaoStartInfo.routeStationId;
      }
      if (this.getBaoEndInfo.routeStationId) {
        params.end_site = this.getBaoEndInfo.routeStationId;
      }
      console.log(params);
      this.$store.dispatch(COST_ESTIMAT_CHARTER, params);
    },
    selectStartTime() {
      this.$store.dispatch(GET_ORDER_TIME, '包车').then(() => {
        Popup.createTimeSelector().then(() => {
          this.getCostInfo();
        });
      });
    },
    selectHighway() {
      if (this.getRoute.hspCostFlag !== '0') {
        return;
      }
      Popup.createHighwaySelector().then(() => {
        this.getCostInfo();
      });
    },
    selectCartype() {
      Popup.createCartypeSelector().then(() => {
        this.getCostInfo();
      });
    },
    selectRemarks() {
      Popup.createRemarkSelector('2').then(() => {
        this.getCostInfo();
      });
    },
    submitOrder() {
      let params = {
        route_id: this.getRoute.route_id,
        order_type: '包车',
        travel_time: this.getBaoStartTime.time.valueOf().toString(),
        origin_coord: encryptByDESModeEBC(this.getBaoStartInfo.coordinate[0] + ',' + this.getBaoStartInfo.coordinate[1]),
        dest_coord: encryptByDESModeEBC(this.getBaoEndInfo.coordinate[0] + ',' + this.getBaoEndInfo.coordinate[1]),
        origin: this.getBaoStartInfo.stationName,
        destination: this.getBaoEndInfo.stationName,
        car_type: this.getBaoCarType,
        speed_status: this.getUseHighway.value,
        remarks: this.getBaoSelectedRemarksString,
        platformId: this.getPlatformId,
        systemType: Utils.getCurrentEnv()
      };
      if (this.getBaoStartInfo.routeStationId) {
        params.start_site = this.getBaoStartInfo.routeStationId;
      }
      if (this.getBaoEndInfo.routeStationId) {
        params.end_site = this.getBaoEndInfo.routeStationId;
      }
      if (this.getSelectedContact) {
        params.passenger_name = this.getSelectedContact.name;
        params.passenger_mobile = encryptByDESModeEBC(this.getSelectedContact.phone);
      }
      this.$store
        .dispatch(ORDER_SUBMIT, params)
        .then(() => {
          this.$router.push({
            name: 'OrderPayment',
            query: {
              orderno: this.getOrderInfoData.order_no
            }
          });
          this.$store.commit('resetRoute');
        })
        .catch(reason => {
          Dialog.alert({
            title: '温馨提示',
            content: reason.message,
            confirmText: '确定'
          });
        });
    },
    gotoCostDetail() {
      // this.$router.push({
      //     name: 'beforechargesDetails',
      //     query: {
      //         orderno: this.getBaoCostInfo.cost_id
      //     }
      // })
      Popup.createChargeDetail(this.getBaoCostInfo.cost_id);
    },
    update() {
      console.log('包车UPDATE');
      if (this.getBaoStartInfo && this.getBaoEndInfo) {
        this.positionSelected = true;
        if (this.getBaoStartTime) {
          this.getCostInfo();
        }
      } else {
        this.positionSelected = false;
        this.hasCostInfo = false;
        this.$store.commit('setBaoCostInfo', null);
      }
    }
  },
  computed: {
    highwayString() {
      // 检查此线路包车是否开启了高速选项
      let str = '';
      if (this.getRoute.hspCostFlag === '0') {
        str = this.getUseHighway ? this.getUseHighway.text : '';
      } else {
        str = '不走高速';
      }
      return str;
    },
    startInfoString() {
      let str = '';
      if (this.getBaoStartInfo) {
        str = this.getRoute.start_auto_name + '  ' + this.getBaoStartInfo.stationName;
      }
      return str;
    },
    endInfoString() {
      let str = '';
      if (this.getBaoEndInfo) {
        str = this.getRoute.end_auto_name + '  ' + this.getBaoEndInfo.stationName;
      }
      return str;
    },
    canGetCostInfo() {
      let result = false;
      if (this.getRoute.shangwuFlag === '1') {
        result = this.getBaoStartInfo && this.getBaoEndInfo && this.getBaoStartTime && this.getBaoCarType !== '';
      } else {
        result = this.getBaoStartInfo && this.getBaoEndInfo && this.getBaoStartTime;
      }
      return result;
    },
    ...mapGetters([
      'getConfirmdRoute',
      'getRoute',
      'getPlatformId',
      'getWaitingRoute',
      'getBaoStartInfo',
      'getBaoEndInfo',
      'getBaoConfirmStartTime',
      'getBaoStartTime',
      'baoNumPassenger',
      'getBaoSelectedRemarks',
      'getBaoCostInfo',
      'getBaoSelectedRemarksString',
      'getOrderInfoData',
      'getUseHighway',
      'getBaoCarType',
      'getBaoAdv',
      'getSelectedContact'
    ])
  }
};
</script>
<style>
.gaomsg {
  color: #ff6917;
}
.gaomsg img {
  width: 26px;
  height: 24px;
}
</style>

